package apportioner

import (
	sqlc "bp/db/sqlc"
)

func (s *ApportionerComServiceImp) genUpsertOffApportionmentParamsPtrsOfSingle(
	offTransactionPtr *sqlc.OffTransaction,
	theMapOfOffTransactionDetail OffTrialItemToOffTransactionDetailMap,
	offTrialPtrs []*sqlc.OffTrial,
	offTrialItemPtrs []*sqlc.OffTrialItem,
) (
	upsertOffApportionmentParamsPtrs []*sqlc.UpsertOffApportionmentParams) {

	apportionScene := string(sqlc.EmOffApportionSceneOrder)
	apportionTypeSingle := string(sqlc.EmOffApportionTypeSingle)
	for _, offTrialPtr := range offTrialPtrs {
		offTrialItemPtrsOfTheOffTrial := s.entityHub.OffTrialService.FilterOffTrialItemPtrs(offTrialItemPtrs, func(oti *sqlc.OffTrialItem) bool {
			return oti.OffTrialID == offTrialPtr.ID
		})
		for _, offTrialItemPtr := range offTrialItemPtrsOfTheOffTrial {
			k := s.ExtractOwnerOffTrialItemKey(offTransactionPtr.OwnerID)(offTrialItemPtr)
			offTransactionDetailPtrsOfMap := theMapOfOffTransactionDetail[k]
			for _, ptr := range offTransactionDetailPtrsOfMap {
				upsertOffApportionmentParamsPtrs = append(upsertOffApportionmentParamsPtrs, &sqlc.UpsertOffApportionmentParams{
					BrandID:                offTransactionPtr.BrandID,
					OwnerType:              offTransactionPtr.OwnerType,
					OwnerID:                offTransactionPtr.OwnerID,
					MemberID:               offTransactionPtr.MemberID,
					TransactionID:          offTransactionPtr.ID,
					OutTransNo:             offTransactionPtr.OutTransNo,
					OutOrderNo:             offTransactionPtr.OutOrderNo,
					OutAfterSaleNo:         offTransactionPtr.OutAfterSaleNo,
					OutOrderDetailID:       ptr.OutOrderDetailID,
					OutOrderDetailCnt:      ptr.OutOrderDetailCnt,
					OutAfterSaleDetailID:   ptr.OutAfterSaleDetailID,
					OutAfterSaleDetailCnt:  ptr.OutAfterSaleDetailCnt,
					ItemID:                 ptr.ItemID,
					ItemSpecID:             ptr.ItemSpecID,
					ItemPrice:              ptr.ItemPrice,
					ApportionScene:         apportionScene,
					ApportionType:          apportionTypeSingle,
					OfferType:              offTrialPtr.OfferType,
					OfferItemType:          offTrialPtr.OfferItemType,
					OfferItemApportionInfo: offTrialPtr.OfferItemTrialInfo,
					Off:                    offTrialPtr.Off,
					Cnt:                    offTrialItemPtr.Cnt,
				})
			}
		}
	}

	return
}